<?php
namespace api\modules\v1\controllers;

use api\common\controllers\Controller;
use common\records\Share;
use common\records\Record;
use common\records\Achievement;
use common\records\UserAchievement;

class UserShareController extends Controller
{
    /**
     * @api {get} /v1/user-share/one 获取指定分享
     * @apiVersion 1.0.0
     * @apiName one
     * @apiGroup user-share
     *
     *
     */
    public function  actionOne()
    {
        $result = array();
        if(!array_key_exists("share_id",$_GET))
        {
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $share_id = trim($_GET['share_id']);
        $share = Share::find()->where(['id'=>$share_id])->asArray()->one();

        if($share == null)
        {
            $detail = array();

            $detail['code'] = 404;
            $detail['reason'] = "not found";
            $result['result'] = $detail;
            return $result;
        }

        $achievement_id = $share['achievement_id'];
        $uid = $share['uid'];
        unset($share['achievement_id']);

        $result['share'] = $share;

        $achievement_ids = explode(",", $achievement_id);
        $res_achievements = array();
        if(count($achievement_ids) > 0)
        {
            $achievements = Achievement::find()->where(['id'=>$achievement_ids])->asArray()->all();
            $user_achievements = UserAchievement::find()->where(['aid'=>$achievement_ids,'uid'=>$uid])->asArray()->all();
            for($i = 0; $i <count($achievements); $i++)
            {
                $achieve = array();

                $achieve['timestamp'] = \common\helpers\Util::tranTime($user_achievements[$i]['timestamp']);
                $achieve['title'] = $achievements[$i]['title'];
                $achieve['description'] = $achievements[$i]['description'];
                $achieve['photo'] = $achievements[$i]['photo'];

                $res_achievements[] = $achieve;
            }
            $result['achievement'] = $res_achievements;
        }
        return $result;
    }
    
}
